package com.ipay.wechat.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.ipay.redis.JedisExecutor;
import com.ipay.redis.JedisKeyPrefix;
import com.ipay.wechat.component.Wechat;
import com.ipay.wechat.core.RequestContext;
import com.ipay.wechat.pojo.AccessToken;
import com.ipay.wechat.pojo.WXTicket;
import com.ipay.wechat.util.AccessTokenUtil;


public class RequestContextInterceptor extends HandlerInterceptorAdapter {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private JedisExecutor manager;
	@Autowired
	private Wechat wechat;
	
	@Value("${wechat.mock}")
	private boolean ismock = false;

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		logger.info("jingafterCompletion");
		RequestContext.set(null);
		super.afterCompletion(request, response, handler, ex);
	}

	@Override
	public void afterConcurrentHandlingStarted(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		logger.info("jingafterConcurrentHandlingStarted");
		super.afterConcurrentHandlingStarted(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		logger.info("jingpostHandle");
		super.postHandle(request, response, handler, modelAndView);
		
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		RequestContext.get(); 
		if(ismock) return super.preHandle(request, response, handler);
		try {
//			AccessTokenUtil tokenUtil=new AccessTokenUtil();
			AccessToken accessToken=new AccessToken();
			String access_token = manager.getValue(JedisKeyPrefix.JEDIS_KEY_PREFIX_WECHAT_TOKEN,"access");
			logger.info("从jedies中取出的accesstoken==="+access_token);
//			long time = Long.parseLong(manager.getValue(JedisKeyPrefix.JEDIS_KEY_PREFIX_WECHAT_TICKET,"time"));
			if(access_token==null){
				logger.info("accesstoken失效");
				accessToken = AccessTokenUtil.freshAccessToken(wechat);
				manager.setValue(JedisKeyPrefix.JEDIS_KEY_PREFIX_WECHAT_TOKEN,"access", accessToken.getAccess_token(), accessToken.getExpire_in());
				
			}
			String ticket = manager.getValue(JedisKeyPrefix.JEDIS_KEY_PREFIX_WECHAT_TICKET,"ticket");
//			long ticketTime = Long.parseLong(manager.getValue(JedisKeyPrefix.JEDIS_KEY_PREFIX_WECHAT_TICKET,"ticketTime"));
			if(ticket == null){
				logger.info("ticke失效");
				WXTicket wxticket = AccessTokenUtil.freshTicket(accessToken.getAccess_token());
				manager.setValue(JedisKeyPrefix.JEDIS_KEY_PREFIX_WECHAT_TICKET, "ticket", wxticket.getTicket(), wxticket.getExpires_in());
			}
		} catch (Exception e) {
			// TODO: handle exception
			logger.info(e.getMessage(),e);
		}
		
		return super.preHandle(request, response, handler);
	}

	
}
